/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dal.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gabriellebotbol
 */
public class Database 
{
    static
    {
        Properties properties=new Properties();
        try 
        {
            try 
            {
                properties.load(Database.class.getResourceAsStream("properties.properties"));
                Class.forName(properties.getProperty("driver"));
                Database.URL=properties.getProperty("connectionString");
                Database.USERNAME=properties.getProperty("username");
                Database.PASSWORD=properties.getProperty("password");
                
            } 
            catch (IOException ex) 
            {
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
        catch (ClassNotFoundException ex) 
        {
            Logger.global.log(Level.SEVERE, null, ex);
        }
        
    }
    private static String URL;
    private static String USERNAME;
    private static String PASSWORD;
    private static Connection con=null;
    
    public static ResultSet executeQuery(String query) throws SQLException
    {
        ResultSet rs=null;
        Statement statement=null; 
        con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        statement = con.createStatement();
        rs = statement.executeQuery(query);
        return rs;        
    }
    
    public static void executeNonQuery(String query) throws SQLException
    {
        Statement statement=null;
        con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        statement = con.createStatement();
        statement.executeUpdate(query);
        con.close();
    }
    
    public static void closeConnection() throws SQLException
    {
        if(con != null && !con.isClosed()) 
        {
            con.close();
        }
    }
            
}
